package com.kunya.product.service.impl.category;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.category.CategoryMapper;
import com.kunya.product.po.Category;
import com.kunya.product.service.category.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;

/**
 * @author zp
 * @Description:
 * @date 2018/5/19 15:03
 */
@Slf4j
@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {


    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * @author zp
     * @Description:创建分类信息
     * @param
     * @date 2018/7/11 14:48
     */
    public ServerResponse addCategory(String categoryName, Long parentId,String tablePrefix) {
        //品类名称或父节点id为空返回错误信息
        if(parentId==null|| StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("添加品类参数错误");
        }
        //组装update sql所需bean
        Category category=new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        Byte status=1;//todo 向常量类中添加“分类”状态
        category.setStatus(status);//新添加分类默认分类可用

        int rowCount=categoryMapper.insert(category,tablePrefix);
        //判断数据是否更新成功
        if(rowCount>0) {
            return ServerResponse.createBySuccessMessage("创建品类成功");
        }
        return ServerResponse.createByErrorMessage("创建品类失败");

    }
    /**
     * @author zp
     * @Description:更新分类
     * @param
     * @date 2018/7/11 14:49
     */
    public ServerResponse updateCategoryName(Long categoryId,String categoryName,String tablePrefix) {
        //品类名称或父节点id为空返回错误信息
        if(categoryId==null||StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("更新品类参数错误");
        }
        Category category=new Category();
        category.setCategoryId(categoryId);
        category.setName(categoryName);
        int rowCount=categoryMapper.updateByPrimaryKeySelective(category,tablePrefix);
        if(rowCount>0) {
            return ServerResponse.createBySuccessMessage("更新品类成功");
        }
        return ServerResponse.createByErrorMessage("更新品类失败");
    }
    /**
     * @author zp
     * @Description: 查询子分类，非递归
     * @param
     * @date 2018/7/11 14:49
     */
    public ServerResponse<List<Category>> getChildrenParallelCategory(Long categoryId,String tablePrefix){
        List<Category> categoryList=categoryMapper.selectCategoryChildrenByParentId(categoryId,tablePrefix);
        if(CollectionUtils.isEmpty(categoryList)) {
//            log.info("未找到当前分类的子分类");
            log.info("未找到当前分类的子分类");
        }
        return ServerResponse.createBySuccess(categoryList);

    }


    /**
     * @author zp
     * @Description: 递归查询子分类
     * @param
     * @date 2018/7/11 14:50
     */
    public ServerResponse<List<Long>> selectCategoryAndChildrenById(Long categoryId,String tablePrefix){
        Set<Category> categorySet = Sets.newHashSet();
        findChildCategory(categorySet,categoryId,tablePrefix);


        List<Long> categoryIdList = Lists.newArrayList();
        if(categoryId != null){
            for(Category categoryItem : categorySet){
                categoryIdList.add(categoryItem.getCategoryId());
            }
        }
        return ServerResponse.createBySuccess(categoryIdList);
    }


    /**
     * @author zp
     * @Description:隐藏分类
     * @param
     * @date 2018/7/11 14:51
     */
    @Override
    public ServerResponse disableCategory(Long categoryId, String tablePrefix) {
        Category category=new Category();
        category.setCategoryId(categoryId);
        category.setStatus((byte)0);
        categoryMapper.updateByPrimaryKeySelective(category,tablePrefix);
        return ServerResponse.createBySuccess("隐藏分类成功");
    }


    /**
     * @author zp
     * @Description: 递归算法,算出子节点
     * @param
     * @date 2018/7/11 14:49
     */
    private Set<Category> findChildCategory(Set<Category> categorySet ,Long categoryId,String tablePrefix){
        Category category = categoryMapper.selectByPrimaryKey(categoryId,tablePrefix);
        if(category != null){
            categorySet.add(category);
        }
        //查找子节点,递归算法
        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId,tablePrefix);
        for(Category categoryItem : categoryList){
            findChildCategory(categorySet,categoryItem.getCategoryId(),tablePrefix);
        }
        return categorySet;
    }
}
